package simu.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;

public class Drive {
	
	private String path; 
	private Writer filein;
	private Reader fileout;
	private Scanner scan;
	private String data = "";
	
	public Drive(String path) throws FileNotFoundException {
		this.path = path;
		File aux = new File(path);
		if (aux.exists()) {
			open();
		} else {
			create();
		}
	}

	public void open() throws FileNotFoundException{
		filein = new OutputStreamWriter(new FileOutputStream(path));
		fileout = new InputStreamReader(new FileInputStream(path));
		scan = new Scanner(System.in);		
	}
	
	public void remove(){
	
	}
	
	public void create() throws FileNotFoundException{
		filein = new OutputStreamWriter(new FileOutputStream(path));
		fileout = new InputStreamReader(new FileInputStream(path));
		scan = new Scanner(System.in);				
	}

	public void write() throws IOException{
		filein.write(data);
		filein.close();
	}
	
	public void read() throws IOException{
		String strLine;
		BufferedReader br = new BufferedReader(fileout);
	  //Read File Line By Line
	  while ((strLine = br.readLine()) != null)   {
		  data += strLine + "\r\n";
	  }
	  //Close the input stream
	  fileout.close();
	}
	
	public String getData() {
		return data;
	}

	public void edit(){
		String last="";
		while (!last.equals("done")) {
			last = scan.nextLine();
			data += last + "\r\n";
		}	
	}
}
	